﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.Framework;
using TjZheng.Web.Helper;
using TjZheng.Web.Mvc;
using TjZheng.WeiXin.SDK;
using TjZheng.WeiXin.SDK.Pay;

namespace TjZheng.WeiXin.Web.Controllers
{
    public class NativePayController : TjZhengController
    {
        /// <summary>
        /// 扫码支付模式一
        /// </summary>
        /// <param name="merchantId"></param>
        /// <returns></returns>
        public ActionResult ModeOne(int? merchantId)
        {
            if (!merchantId.HasValue)
            {
                return Http404();
            }

            NativePay objNativePay = new PayModeOne(merchantId.Value);

            WxPayPackage objWxPayPackage = new WxPayPackage();
            objWxPayPackage.SetValue("product_id", "123456");//商品ID

            return Json(objNativePay.CreatePayUrl(objWxPayPackage), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 扫码支付模式一回调地址，配置路径：微信公众号平台-微信支付-扫码支付
        /// </summary>
        /// <returns></returns>
        public ActionResult ModeOneCallBack()
        {
            //todo：生成订单
            //todo：统一下单

            return View();
        }

        /// <summary>
        /// 扫码支付模式二
        /// </summary>
        /// <param name="merchantId"></param>
        /// <returns></returns>
        public ActionResult ModeTwo(int? merchantId)
        {
            if (!merchantId.HasValue)
            {
                return Http404();
            }

            NativePay objNativePay = new PayModeTwo(merchantId.Value);

            WxPayPackage objWxPayPackage = new WxPayPackage();
            objWxPayPackage.SetValue("body", "test");//商品描述
            objWxPayPackage.SetValue("attach", "test");//附加数据
            objWxPayPackage.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo(objNativePay.MerchantInfo.MChId));//随机字符串
            objWxPayPackage.SetValue("total_fee", 1);//总金额
            objWxPayPackage.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));//交易起始时间
            objWxPayPackage.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));//交易结束时间
            objWxPayPackage.SetValue("goods_tag", "jjj");//商品标记
            objWxPayPackage.SetValue("trade_type", "NATIVE");//交易类型
            objWxPayPackage.SetValue("product_id", "123456");//商品ID
            objWxPayPackage.SetValue("spbill_create_ip", SystemWebAsset.GetServerIP());//终端ip	  	    
            objWxPayPackage.SetValue("nonce_str", WxPayApi.GenerateNonceStr());//随机字符串
            objWxPayPackage.SetValue("appid", objNativePay.MerchantInfo.AppId);//公众账号ID
            objWxPayPackage.SetValue("mch_id", objNativePay.MerchantInfo.MChId);//商户号      

            //异步通知url未设置，则使用配置文件中的url
            if (!objWxPayPackage.IsValueSet("notify_url"))
            {
                objWxPayPackage.SetValue("notify_url", objNativePay.MerchantInfo.WxNotifyUrl);//异步通知url
            }
            objWxPayPackage.SetValue("sign", objWxPayPackage.CreateSign(objNativePay.MerchantInfo.MKey));//签名

            return Json(objNativePay.CreatePayUrl(objWxPayPackage), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 支付成功微信支付系统异步消息通支付结果
        /// </summary>
        /// <returns></returns>
        public ActionResult PaySuccessNotify()
        {
            NotifyProcessContext objNotifyProcessContext = new NotifyProcessContext();
            WxPayPackage objWxPayPackage = WxPayPackage.ParseFromStream(Request.InputStream);
            objNotifyProcessContext.SetIntputWxPayPackage(objWxPayPackage);

            NotifyProcessHandle objNotifyProcessHandle = new NotifyProcessHandle();
            InvokeResult objInvokeResult = objNotifyProcessHandle.ExecuteProcess(objNotifyProcessContext);
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

    }
}
